//
//  XHRSettinsViewController.m
//  BuDeJie
//
//  Created by haoran on 16/4/3.
//  Copyright © 2016年 xuhaoran. All rights reserved.
//

#import "XHRSettinsViewController.h"
#import <SDImageCache.h>
#import "XHRFileManager.h"
#define cachePath NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]
static NSString * const ID = @"cell";

@implementation XHRSettinsViewController

-(void)viewDidLoad{

    //设置标题
    self.title = @"设置";

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"jump" style:UIBarButtonItemStyleDone target:self action:@selector(jump)];
    
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];

}

//跳转到一个控制器
-(void)jump{
    
    UIViewController *vc = [[UIViewController alloc]init];
    
    vc.view.backgroundColor = [UIColor redColor];
    
    [self.navigationController pushViewController:vc animated:YES];
}

//返回上一层
-(void)back{

    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark ---- 数据源方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    
    // 获取缓存的尺寸 -> SDWebImage和WKWebView
    // SDWebImage:怎么做缓存 , 沙盒:Cache
    NSInteger fileSize = [[SDImageCache sharedImageCache] getSize];
    
  
    
    //确定缓存路径
//    NSString * cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    NSLog(@"%ld --- %@",fileSize,cachePath);
    
    cell.textLabel.text =[self getFileSizeStr];
    return cell;

}


// 获取文件尺寸字符串
-(NSString *)getFileSizeStr{

    // b -> b / 1000 KB -> KB / 1000 MB
    //获取Cache路径
//    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    
    NSInteger totalSize = [XHRFileManager getFileSize:cachePath];
    NSString * str = @"清除缓存";
    
    if ( totalSize >= 1000*1000 ) {//MB
        
        CGFloat temp = totalSize / 1000.0 / 1000.0;
        str = [NSString stringWithFormat:@"%@(%.1fMB)",str,temp];
        
    }else if ( totalSize >= 1000 && totalSize < 1000 * 1000 ){ //KB
    
        CGFloat temp = totalSize / 1000.0;
        str = [NSString stringWithFormat:@"%@(%.1fKB)",str,temp];
    }else if ( totalSize > 0 ){
        
        str = [NSString stringWithFormat:@"%@(%zdB)",str,totalSize];
    }
    
    return str;
    
}




#pragma mark ---- 点击cell调用
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [XHRFileManager removeDirectoryPath:cachePath];
    //刷新表格
    [self.tableView reloadData];
}

@end
